//
//  LocationManager.m
//  ImGuider
//
//  Created by llt on 2017/4/11.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "LocationManager.h"
#import <UIKit/UIKit.h>

@interface LocationManager ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *cLocation;

@property (nonatomic, copy) void (^ realTimeLocation) (CLLocation *location, NSError *error);

@end
@implementation LocationManager

- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        IGLog(@"----------------------------------ff----------------------------------");
        
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = 10.0f; //移动10米更新位置
        _locationManager.pausesLocationUpdatesAutomatically = NO;
        //定位服务是否可用
        BOOL enable=[CLLocationManager locationServicesEnabled];
        //是否具有定位权限
        int status=[CLLocationManager authorizationStatus];
        
        if (status == 2) {
            
            [IGAlertView alertWithTitle:@"" message:MYLocalizedString(@"PUBLIC_NOGPS_ALERT_CONTENT", nil) cancelButtonTitle:MYLocalizedString(@"PUBLIC_CANCEL", nil) commitBtn:MYLocalizedString(@"PUBLIC_NOGPS_ALERT_COMMIT", nil) commit:^{
                
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                [[UIApplication sharedApplication] openURL:url];
            } cancel:nil];

        } else if(!enable || status<3){
            //请求权限
            [_locationManager requestWhenInUseAuthorization];
        }
    }
    return self;
}

+ (instancetype)sharedManager {
    
    static LocationManager *locManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        locManager = [[LocationManager alloc] init];
    });
    
    return locManager;
}

- (void)startUpdatingLocation {
    
    [self.locationManager startUpdatingLocation];
}

- (CLLocation *)currentLocation {
    
    return _cLocation;
}

- (void)realLocation:(void (^)(CLLocation *location, NSError *error))block {
    
    self.realTimeLocation = block;
    
    if (self.realTimeLocation && self.cLocation) {
        
        self.realTimeLocation(self.cLocation, nil);
    }
}


- (CLLocationDistance)distanceCoordinate:(CLLocationCoordinate2D)coordinate {
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    
    return [self.cLocation distanceFromLocation:location];
}


#pragma mark -CLLocationManagerDelegate 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
    if (self.realTimeLocation) {
        
        self.realTimeLocation(newLocation,nil);
    }
    self.cLocation = newLocation;
}

- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region
              withError:(NSError *)error {
    
    if (self.realTimeLocation) {
        
        self.realTimeLocation(nil,error);
    }
}

@end
